package com.ruoyi.crop.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.ruoyi.crop.domain.DaoDept;
import com.ruoyi.crop.mapper.DaoDeptMapper;

import java.util.List;

public class DaoDataListener implements ReadListener<DaoDept> {
    private static final int BATCH_COUNT = 3;
    private List<DaoDept> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    private DaoDeptMapper daoDeptMapper;

    public DaoDataListener(DaoDeptMapper daoDeptMapper) {
        this.daoDeptMapper = daoDeptMapper;
    }

    @Override
    public void invoke(DaoDept daoDept, AnalysisContext analysisContext) {
        System.out.println("解析出来的数据"+daoDept);
        cachedDataList.add(daoDept);
        if(cachedDataList.size() >= BATCH_COUNT){
            daoDeptMapper.saveData(cachedDataList);
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("解析完成");
        if(cachedDataList.size() >= 1){
            daoDeptMapper.saveData(cachedDataList);
        }
    }
}
